package com.controller;

import com.util.FileUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.URLDecoder;
import java.util.List;

/**
 * �ļ��ϴ�������
 * 
 * @author Pudding
 * 
 */
@Controller("UploadController")
@RequestMapping("/upload.do")
public class UploadController {
	private String separator = System.getProperty("file.separator");

	/**
	 * 活动图片上传
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "uploadPicture")
	public void uploadHuodong(HttpServletRequest request,
                              HttpServletResponse response, HttpSession session) throws Exception {
		// ��ȡ����������·��
		String servletPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "article");

		String tempPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "temp");
		// String myPath =
		// "D:\\workspace\\DTCardWeiXin\\src\\main\\webapp\\resources\\images\\";
		response.setContentType("text/html;charset=utf-8");
		String name = "";
		String newFileName = "";
		System.out.println("name:1");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 璁剧疆涓存椂鏂囦欢鐩綍
		factory.setRepository(new File(tempPath));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		@SuppressWarnings("unchecked")
		List<FileItem> items = upload.parseRequest(request);
		for (FileItem item : items) {
			name = item.getName();
			System.out.println("name:" + name);
			if (name == null || name.trim().equals("")) {
				continue;
			}
			newFileName = FileUtils.commonFileUpload(item, servletPath).get(
					"file");
		}
		String[] json = { newFileName };
		response.getWriter().print(JSONArray.fromObject(json));
	}

	/**
	 * 活动输出图片预览
	 * 
	 * @param filePath
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "readPicture")
	public void readHuodong(String filePath, HttpServletResponse response,
			HttpSession session) throws Exception {
		String path = URLDecoder.decode(filePath, "utf-8");
		System.out.println("path" + path);
		// ��ȡ����������·��
		String servletPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "article");
		FileUtils.getPicPreview(response, servletPath + separator + path,
				FileUtils.ImageType.jpeg);
	}

	/**
	 * 删除图片
	 * 
	 * @param filePath
	 * @param response
	 * @throws Exception
	 */

	@RequestMapping(params = "method=delete")
	public void delete(HttpSession session, String filePath, HttpServletResponse response) throws Exception {
		String url = session.getServletContext().getRealPath(
				separator + "images" + separator + "article");
		response.setContentType("text/html;charset=utf-8");
//		System.out.println(":::filePath:::"+filePath+":::url:::"+url);
		JSONObject json = new JSONObject();
		try {
			String path = URLDecoder.decode(filePath, "utf-8");
//			System.out.println(":::path:::"+path);
			FileUtils.deleteFile(url + separator, path);
			json.put("result", "y");
			json.put("msg", "删除成功");
		} catch (Exception e) {
			json.put("result", "n");
			json.put("msg", "删除失败");
			e.printStackTrace();
		}
		response.getWriter().print(json);
	}

	/**
	 * 广告图片上传
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "uploadAd")
	public void uploadAd(HttpServletRequest request,
                         HttpServletResponse response, HttpSession session) throws Exception {
		// ��ȡ����������·��
		String servletPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "ad");

		String tempPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "temp");
		// String myPath =
		// "D:\\workspace\\DTCardWeiXin\\src\\main\\webapp\\resources\\images\\";
		response.setContentType("text/html;charset=utf-8");
		String name = "";
		String newFileName = "";
		System.out.println("name:1");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 璁剧疆涓存椂鏂囦欢鐩綍
		factory.setRepository(new File(tempPath));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		@SuppressWarnings("unchecked")
		List<FileItem> items = upload.parseRequest(request);
		for (FileItem item : items) {
			name = item.getName();
			System.out.println("name:" + name);
			if (name == null || name.trim().equals("")) {
				continue;
			}
			newFileName = FileUtils.commonFileUpload(item, servletPath).get(
					"file");
		}
		String[] json = { newFileName };
		response.getWriter().print(JSONArray.fromObject(json));
	}

	/**
	 * 广告图片预览
	 * 
	 * @param filePath
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "readAd")
	public void readAd(String filePath, HttpServletResponse response,
			HttpSession session) throws Exception {
		String path = URLDecoder.decode(filePath, "utf-8");
		System.out.println("path" + path);
		// ��ȡ����������·��
		String servletPath = session.getServletContext().getRealPath(
				separator + "images" + separator + "ad");
		FileUtils.getPicPreview(response, servletPath + separator + path,
				FileUtils.ImageType.jpeg);
	}

}
